const { formatResponse } = require("../utils/tool");
const { default: axios } = require("axios");
const { visitorInfoSittingDao } = require("../dao/visitorInfoDao");
//获取访问者的位置信息
module.exports.getIpService = async function (ip) {
  console.log(ip, "ipv4");
  //获取真是ip
  // let { data: realIp } = await axios.get(`https://api.ipify.org/`)
  //腾讯定位,缺省ip时返回的是本地ip
  return axios
    .get(
      `https://apis.map.qq.com/ws/location/v1/ip?ip=${ip}&key=TTTBZ-AGKED-3344T-PTAZ6-TKVDH-YOFBF`
    )
    .then(async (response) => {
      if (response.data.status == 0) {
        console.log(ip, "<---realIp");
        let adInfoOb = {
          nation: response.data.result.ad_info.nation,
          province: response.data.result.ad_info.province,
          city: response.data.result.ad_info.city,
          district: response.data.result.ad_info.district,
          adcode: response.data.result.ad_info.adcode,
          nation_code: response.data.result.ad_info.nation_code,
          lat: response.data.result.location.lat,
          lng: response.data.result.location.lng,
          lastTime: Date.now(),
          totNum: 1,
        };
        //存储访问者的ip地址
        await visitorInfoSittingDao(ip,adInfoOb);
        return formatResponse(0, "ip查询成功", response.data);
      }
      console.log(ip, "<---realIp");
      return formatResponse(response.data.status, response.data.message, {});
    });
};
//存储访问者的信息
module.exports.setIpService = async function (ip,adInfoOb) {
  //存储访问者的ip地址
  await visitorInfoSittingDao(ip,adInfoOb);
  return formatResponse(0, "访客记录存储成功！", null);
};
